package com.c.ctgproject.ui.collect

import android.content.Intent
import android.view.View
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityHandlerBatteryBinding
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.event.CarListBatteryOilEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/9
 *描述: 拆电池 页面(预处理) 电池拆解 页面(预处理)
 */
class HandlerBatteryActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityHandlerBatteryBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val mCarId by lazy { intent.getIntExtra(MyConstants.CAR_ID, -1).handlerNull() }

    //有无电池
    private var hasBattery = false

    //是否直接拆解完成
    private var handlerFinish = false
    private var batteryName = ""


    private val imageShowList = ArrayList<String>()
    private val imageResultCode = 100
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION, -1) }
    override fun event() {
        mBinding.mPhotoNGV.init(this, imageShowList, 6, imageResultCode)
        //进页面默认无电池 先不展示
        mBinding.mLayout.gone()
        mBinding.mImageSelect.gone()
        mBinding.stockNumStart.gone()
        mBinding.moduleCountLL.gone()
        mBinding.moduleCountLine.gone()
        mBinding.mHandlerFinishRG.gone()
    }

    //    private var enterMode=1
    override fun click() {
        mBinding.voltage.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.capacityAh.text.handlerNull())
            result?.let {
                mBinding.capacityKWh.textSet(result)
            }
        }
        mBinding.capacityAh.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.voltage.text.handlerNull())
            result?.let {
                mBinding.capacityKWh.textSet(result)
            }
        }
        mBinding.capacityKWh.handlerDecimal3()

        //选择电池名称
        mBinding.mDictionarySelectLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.BATTERY.menuCode, true, arrayListOf(batteryName))
        }

        //是否直接拆解完成
        mBinding.mCHAIY.setOnClickListener {
            handlerFinish = true
            mBinding.stockNumStart.visible()
            mBinding.moduleCountLL.visible()
            mBinding.moduleCountLine.visible()

        }
        mBinding.mCHAIN.setOnClickListener {
            handlerFinish = false
            mBinding.stockNumStart.gone()
            mBinding.moduleCountLL.gone()
            mBinding.moduleCountLine.gone()
        }
        mBinding.mYDC.setOnClickListener {
            hasBattery = true
            mBinding.mLayout.visibility = View.VISIBLE
            mBinding.mImageSelect.visibility = View.VISIBLE
            mBinding.mHandlerFinishRG.visible()

        }
        mBinding.mWDC.setOnClickListener {
            hasBattery = false
            mBinding.mLayout.visibility = View.INVISIBLE
            mBinding.mImageSelect.visibility = View.INVISIBLE
            mBinding.mHandlerFinishRG.gone()

        }

//        mBinding.mCJ.setOnClickListener {
//            enterMode=1
//        }
//        mBinding.mSJ.setOnClickListener {
//            enterMode=2
//        }

        mBinding.mConfirmBTN.setOnClickListener {
            if (!hasBattery) {
                //没电池直接走接口
                val map = HashMap<String, Any>()
                map["carId"] = mCarId
                ApiMethods.apiSubscribe(ApiMethods.retrofit.handlerBattery(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("无电池拆解电池完成回调:$it")
                    showToast("提交成功")
                    val event = CarListBatteryOilEvent()
                    event.position = mListPosition
                    event.battery = true
                    EventBus.getDefault().post(event)
                    finish()
                })
            } else {
                when {
                    hasBattery && batteryName.isEmpty() -> showToast("请选择电池名称")
                    handlerFinish && mBinding.stockNum.text.isEmpty() -> showToast("请输入数量")
                    handlerFinish && mBinding.moduleCount.text.isEmpty() -> showToast("请输入模组个数")

//                mBinding.batteryCode.text.handlerNull().isEmpty()->showToast("请输入电池包编码")
//                mBinding.capacityAh.text.handlerNull().isEmpty()->showToast("请输入容量")
                    else -> {
                        if (imageShowList.isEmpty()) {
                            showToast("请添加照片")
                        } else {
                            val fileList = ArrayList<File>()
                            var index = 0
                            imageShowList.forEach {
                                index++
                                val file = File(it)
//                            val newFile = ImageUtil.imageWaterMark(this, file, index)
                                val newFile: File = WaterRemark.handlerWaterRemark2File(this, file, index.toString())
                                fileList.add(newFile)
                            }
                            upDataFile(fileList) {
                                val dataList = ArrayList<String>()
                                it?.forEach { b ->
                                    dataList.add(b.fileUrl.handlerNull())
                                }
                                handlerHttp(dataList)
                            }
                        }
                    }
                }
            }
        }
    }

    private fun handlerHttp(imageList: ArrayList<String>) {
        val map = HashMap<String, Any>()
        map["carId"] = mCarId
        map["batteryName"] = mBinding.stockName.text.handlerNull()
        map["batteryCode"] = mBinding.batteryCode.text.handlerNull()
        map["count"] = mBinding.stockNum.text.handlerNull()
        map["weight"] = mBinding.weight.text.handlerNull()
        map["capacityAh"] = mBinding.capacityAh.text.handlerNull()
        map["voltage"] = mBinding.voltage.text.handlerNull()
//                    map["moduleCode"]=mBinding.moduleCode.text.handlerNull()
//                    map["moduleCount"]=mBinding.moduleNum.text.handlerNull()
        map["capacityKwh"] = mBinding.capacityKWh.text.handlerNull()
        map["remark"] = mBinding.mRemarkET.text.handlerNull()

        if (handlerFinish) {
            map["moduleCount"] = mBinding.moduleCount.text.handlerNull()
            map["dismantled"] = 1
        } else {
            map["dismantled"] = 0
        }



        map["picUrls"] = imageList
//            map["enterMode"]=enterMode
        ApiMethods.apiSubscribe(ApiMethods.retrofit.handlerBattery(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
            MyLog.d("拆解电池完成回调:$it")
            showToast("提交成功")
            val event = CarListBatteryOilEvent()
            event.position = mListPosition
            event.battery = true
            EventBus.getDefault().post(event)
            finish()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let { result ->
            when (requestCode) {
                1000 -> {
                    //字典回调
                    val name = result.getStringExtra(MyConstants.NAME)
//                val abbreviation=result.getStringExtra(MyConstants.ABBREVIATION)
                    batteryName = name.handlerNull()
                    mBinding.stockName.text = name.handlerNull()
                }
                imageResultCode -> {
                    MyLog.d("图片图片")
                    val list: List<ImageBean>? = result.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    val resultList: MutableList<NineGridBean> = ArrayList()
                    list?.forEach { imageBean ->
                        val nineGirdData = NineGridBean(imageBean.imagePath)
                        imageShowList.add(imageBean.imagePath)
                        resultList.add(nineGirdData)
                    }
                    mBinding.mPhotoNGV.addDataList(resultList)
                }
            }
        }
    }
}